home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / DINKDEMO / DC_SCRIB / DSCRIBBL.C1 < prev    next >
Text File  |  1992-07-23  |  4KB  |  190 lines

  1. /*
  2.     File:        DScribbleDoc.c
  3.  
  4.     Written by:    Mark Gross
  5.  
  6.     Copyright:    ⌐ 1992 by Applied Technical Software, all rights reserved.
  7.     Use at your own risk.
  8.  
  9. */
  10. //This is the diffinition of the DScribbleDocument Class
  11.  
  12. #include "DScribbleDoc.h"
  13. #include "DScribbleWind.h"
  14.  
  15. DDocument* DScribbleDoc :: Init( Boolean OpenFromFile)
  16. {
  17.     DDocument *inheritedDoc;
  18.     
  19.     fPict = NULL;
  20.     
  21.     inheritedDoc = inherited :: Init(OpenFromFile);
  22.  
  23.      return inheritedDoc;
  24. }
  25.  
  26. DScribbleDoc :: ~DScribbleDoc()
  27. {
  28.     if( fPict != NULL)
  29.         KillPicture( fPict);
  30.     if(fPictHeader != NULL)
  31.         DisposHandle(fPictHeader);
  32. }
  33.  
  34. void DScribbleDoc::AEInitDoc(FSSpec *theFSS)
  35. {
  36.     fPict = NULL;
  37.  
  38.     inherited::AEInitDoc(theFSS);        
  39. }//end member function AEInitDoc
  40.  
  41.  
  42.  void DScribbleDoc :: HandleMenuChoice(short menuID, short menuItem)
  43.  {
  44.      if(menuID == rPenMenu)
  45.     {
  46.         switch(menuItem)
  47.         {
  48.             case i1X1:
  49.                 ((DScribbleWind *)fDWindow)->fPenSize = 1;
  50.                 break;
  51.             case i2X2:
  52.                 ((DScribbleWind *)fDWindow)->fPenSize = 2;
  53.                 break;
  54.             case i3X3:
  55.                 ((DScribbleWind *)fDWindow)->fPenSize = 3;
  56.                 break;
  57.             case iBlack:
  58.                 ((DScribbleWind *)fDWindow)->fPenPat = patBlack;
  59.                 break;
  60.             case iGray:
  61.                 ((DScribbleWind *)fDWindow)->fPenPat = patGray;
  62.                 break;
  63.             case iWhite:
  64.                 ((DScribbleWind *)fDWindow)->fPenPat = patWhite;
  65.                 break;
  66.             default:
  67.                 return ;
  68.         }/*end switch menuitem*/
  69.         return ;
  70.     }
  71.     else
  72.     {
  73.         inherited::HandleMenuChoice(menuID, menuItem);
  74.     }
  75. }// end of HandleMenuChoice member fuction
  76.  
  77.  
  78.              
  79.  void DScribbleDoc :: SetUpMenues(void)
  80.  {
  81.     MenuHandle    menu;
  82.     
  83.     menu = GetMHandle(rPenMenu);
  84.  
  85.     EnableMenuItem( menu, i1X1, TRUE);
  86.     EnableMenuItem( menu, i2X2, TRUE);
  87.     EnableMenuItem( menu, i3X3, TRUE);
  88.     EnableMenuItem( menu, iBlack, TRUE);
  89.     EnableMenuItem( menu, iGray, TRUE);
  90.     EnableMenuItem( menu, iWhite, TRUE);
  91.  
  92.     CheckItem(menu,i1X1, ((DScribbleWind *)fDWindow)->fPenSize == 1);
  93.     CheckItem(menu,i2X2, ((DScribbleWind *)fDWindow)->fPenSize == 2);
  94.     CheckItem(menu,i3X3, ((DScribbleWind *)fDWindow)->fPenSize == 3);
  95.     CheckItem(menu, iBlack, ((DScribbleWind *)fDWindow)->fPenPat == patBlack);
  96.     CheckItem(menu, iGray, ((DScribbleWind *)fDWindow)->fPenPat == patGray);
  97.     CheckItem(menu, iWhite, ((DScribbleWind *)fDWindow)->fPenPat == patWhite);
  98.     
  99.     inherited::SetUpMenues();
  100. }// end of SetUpMenues function
  101.  
  102.  
  103. DWindow* DScribbleDoc::MakeWindow(Boolean hasColorWindows)
  104. {    
  105.     DScribbleWind *newWindow;
  106.     
  107.     //newWindow = new DWindow;
  108.     newWindow = new DScribbleWind;
  109.     fDWindow = newWindow;
  110.         
  111.     if (newWindow->Init(this, hasColorWindows))
  112.         return newWindow;
  113.     else
  114.     {
  115.         fDWindow = NULL;
  116.         return fDWindow;
  117.     }    
  118. }//end member function MakeWindow
  119.  
  120.  
  121. OSErr DScribbleDoc :: ReadData(short refNum, long *size)
  122. {
  123.     OSErr fileError;
  124.     Handle data;
  125.     long headerLength;
  126.     
  127.     headerLength = 512;
  128.  
  129.     fPictHeader = NewHandle(headerLength);
  130.     if(fPictHeader == NULL)
  131.         return FALSE;
  132.     
  133.     HLock(fPictHeader);
  134.     fileError = FSRead(refNum, &headerLength, *fPictHeader);
  135.     HUnlock(fPictHeader);
  136.     
  137.     if(fileError != noErr)
  138.     {
  139.         DisposHandle( fPictHeader);
  140.         return fileError;
  141.     }
  142.     
  143.     *size -= headerLength;
  144.  
  145.     fPict = (PicHandle)NewHandle(*size);
  146.     if(fPict == NULL)
  147.         return FALSE;
  148.     
  149.     HLock(fPict);
  150.     fileError = FSRead(refNum, size, *fPict);
  151.     HUnlock(fPict);
  152.     
  153.     if(fileError != noErr)
  154.     {
  155.         DisposHandle( fPict);
  156.         return fileError;
  157.     }
  158.     
  159.     fFileRef = refNum;
  160.     return fileError;    
  161. }
  162.  
  163.  
  164. OSErr DScribbleDoc :: WriteData(short refNum)
  165. {
  166.     OSErr fileError;
  167.     Handle data;
  168.     long dataCount;
  169.  
  170.     data = (Handle) fPict;
  171.     if(data == NULL)
  172.         return FALSE;
  173.     
  174.     // first write out the header info
  175.     data = fPictHeader;
  176.     dataCount = 512;
  177.     HLock(data);
  178.     fileError = FSWrite(refNum, &dataCount, *data);
  179.     HUnlock(data);
  180.  
  181.     // next write out the pict itself
  182.     data = (Handle) fPict;
  183.     dataCount = GetHandleSize(data);    
  184.     HLock(data);
  185.     fileError = FSWrite(refNum, &dataCount, *data);
  186.     HUnlock(data);
  187.     
  188.     return fileError;    
  189. }
  190.